package server

import (
	"fmt"
	"go-stage/core"
	"go-stage/micro/pb"
	"go-stage/micro/system/model"
	"go-stage/micro/system/store"

	"golang.org/x/net/context"
)

type dictService struct{}

var DictService = dictService{}

func (dictService dictService) FindByUUID(ctx context.Context, in *pb.FindByIdRequest) (*pb.DictModel, error) {
	if nil == in {
		return nil, fmt.Errorf("param is empty")
	}
	result, err := store.DictStore.FindByUUID(in.Uuid)
	if err != nil {
		core.Log.Errorf("dict.findByUUID error: %v", err)
		return nil, err
	}
	return result.ToPB(), nil
}

func (dictService dictService) FindChildrenByUUID(ctx context.Context, in *pb.FindByIdRequest) (*pb.DictModelArray, error) {
	if nil == in {
		return nil, fmt.Errorf("param is empty")
	}
	result, err := store.DictStore.FindChildrenByUUID(in.Uuid)
	if err != nil {
		core.Log.Errorf("dict.findChildrenByUUID error: %v", err)
		return nil, err
	}
	resp := make([]*pb.DictModel, 0)
	for _, val := range result {
		resp = append(resp, val.ToPB())
	}
	return &pb.DictModelArray{DictModels: resp}, nil
}

func (dictService dictService) FindGroupByUUID(ctx context.Context, in *pb.FindByIdRequest) (*pb.DictModelArray, error) {
	if nil == in {
		return nil, fmt.Errorf("param is empty")
	}
	result, err := store.DictStore.FindGroupByUUID(in.Uuid)
	if err != nil {
		core.Log.Errorf("dict.findGroupByUUID error: %v", err)
		return nil, err
	}
	resp := make([]*pb.DictModel, 0)
	for _, val := range result {
		resp = append(resp, val.ToPB())
	}
	return &pb.DictModelArray{DictModels: resp}, nil
}

func (dictService dictService) Insert(ctx context.Context, in *pb.DictModel) (*pb.DictModel, error) {
	if nil == in {
		return nil, fmt.Errorf("param is empty")
	}
	result, err := store.DictStore.Insert(&model.DictModel{
		Describe:   in.Describe,
		Context:    in.Context,
		Value:      in.Value,
		ParentUuid: in.ParentUuid,
		GroupUuid:  in.GroupUuid,
		Status:     in.Status,
	})
	if err != nil {
		core.Log.Error("dict.insert error: %v", err)
		return nil, err
	}
	return result.ToPB(), nil
}

func (dictService dictService) Update(ctx context.Context, in *pb.DictModel) (*pb.DictModel, error) {
	if nil == in {
		return nil, fmt.Errorf("param is empty")
	}
	result, err := store.DictStore.Update(&model.DictModel{
		Id:         in.Id,
		Uuid:       in.Uuid,
		Describe:   in.Describe,
		Context:    in.Context,
		Value:      in.Value,
		ParentUuid: in.ParentUuid,
		GroupUuid:  in.GroupUuid,
		Status:     in.Status,
	})
	if err != nil {
		core.Log.Error("dict.update error: %v", err)
		return nil, err
	}
	return result.ToPB(), nil
}

func (dictService dictService) Remove(ctx context.Context, in *pb.DictModel) (*pb.DictModel, error) {
	if nil == in {
		return nil, fmt.Errorf("param is empty")
	}
	result, err := store.DictStore.Remove(in.Uuid)
	if err != nil {
		core.Log.Error("dict.remove error: %v", err)
		return nil, err
	}
	return result.ToPB(), nil
}

func (dictService dictService) FindRootByUUID(ctx context.Context, in *pb.FindByIdRequest) (*pb.DictModel, error) {
	if nil == in {
		return nil, fmt.Errorf("param is empty")
	}
	result, err := store.DictStore.FindRootByUUID(in.Uuid)
	if err != nil {
		core.Log.Errorf("dict.findByUUID error: %v", err)
		return nil, err
	}
	return result.ToPB(), nil
}
